﻿using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfance;
using B.S.BasicData.Read.Apilication.Common.Exec;
using B.S.BasicData.Read.DTOS;
using MediatR;

namespace B.S.BasicData.Read.Apilication.Handler.Exec
{
    public class GetleaveDetailCommonHandle:IRequestHandler<GetleaveDetailCommon,ApiResult<List<LeavePortDto>>>
    {
        private readonly IBaseRepository<LeaveModel> leaveDal;
        private readonly IBaseRepository<UserModel> UserDal;
        public GetleaveDetailCommonHandle(IBaseRepository<LeaveModel> leaveDal, IBaseRepository<UserModel> userDal)
        {
            this.leaveDal = leaveDal;
            UserDal = userDal;
        }

        public async Task<ApiResult<List<LeavePortDto>>> Handle(GetleaveDetailCommon request, CancellationToken cancellationToken)
        {
            ApiResult<List<LeavePortDto>> res = new ApiResult<List<LeavePortDto>>();
            var users = UserDal.GetValue(request.UserId).Result;
            if (users == null)
            {
                res.Code = ApiEnum.Error;
                res.Message = "用户不存在";
                return await Task.FromResult(res);
            }
            var leaveRecords = leaveDal.GetAll()
                .Where(x => x.UserId == request.UserId && x.leaveTime.Month == request.Month).Select(x => new LeavePortDto
                {
                    UserId = request.UserId,
                    leaveName = users.UserName,
                    Month = request.Month,
                    leaveTime = x.leaveTime,
                    EndleaveTime = x.EndleaveTime,
                    Number = x.Number,
                    cause = x.cause,
                }).ToList();
            res.Data = leaveRecords;
            res.Message = "返回明细数据";
            res.Code = ApiEnum.Success;
            return await Task.FromResult(res);
        }
    }
}
